//给你一个包含 n 个整数的数组 nums，判断 nums 中是否存在三个元素 a，b，c ，使得 a + b + c = 0 ？请你找出所有满足条件且不重复
//的三元组。
//
// 注意：答案中不可以包含重复的三元组。
//
//
//
// 示例：
//
// 给定数组 nums = [-1, 0, 1, 2, -1, -4]，
//
//满足要求的三元组集合为：
//[
//  [-1, 0, 1],
//  [-1, -1, 2]
//]
//
// Related Topics 数组 双指针
// 👍 2685 👎 0

fn main() {
    let res = Solution::three_sum(vec![]);
    for list in res {
        println!("{},{},{}", list[0], list[1], list[2])
    }
}

struct Solution;

//leetcode submit region begin(Prohibit modification and deletion)
impl Solution {
    pub fn three_sum(nums: Vec<i32>) -> Vec<Vec<i32>> {
        let mut res: Vec<Vec<i32>> = Vec::new();
        if nums.len() == 0 {
            return res;
        }
        let mut numss = nums;
        numss.sort();

        for first in 0..numss.len() - 1 {
            if first > 0 && numss[first] == numss[first - 1] {
                continue;
            }
            let target = -numss[first];
            let mut third = numss.len() - 1;
            for second in first + 1..numss.len() - 1 {
                if second > first + 1 && numss[second] == numss[second - 1] {
                    continue;
                }
                while second < third && target < numss[second] + numss[third] {
                    third -= 1;
                }
                if second == third {
                    break;
                }
                if target == numss[second] + numss[third] {
                    res.push(vec![numss[first], numss[second], numss[third]])
                }
            }
        }
        res
    }
}
//leetcode submit region end(Prohibit modification and deletion)
